import React, { useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom';
import { Tabs, NavBar, Button, Input, Cell, Dialog } from 'react-vant'
import { Uploader } from '@nutui/nutui-react';
import $ from '../../axios'

export default function Bu() {
    const navgiate = useNavigate()
    let [biaot, setBiao] = useState("")
    let [neir, setNeir] = useState("")
    let [lj, setLj] = useState("")
    let [fda, setFda] = useState([])
    let [fid, setFid] = useState([])
    let [cid, setCid] = useState("")
    const [visible, setVisible] = useState(false)
    const tijiao = async () => {
        let data = {
            title: biaot,
            connect: neir,
            img: lj,
            Fid: cid
        }
        console.log(data);
        await $.post("/d_add", data).then(() => {
            window.location.reload()
        })
    }
    const getfdata = async () => {
        await $.get("/glod_get").then((data) => {
            setFda(data.data.data)
        })
    }
    const jsfollow = () => {
        const Fid = fda.filter(el => el.follow == 1)
        setFid(Fid)
    }

    useEffect(() => {
        getfdata()
    }, [])
    useEffect(() => {
        jsfollow()
    }, [fda])
    return (
        <div>
            <div className="b-head">
                <NavBar
                    title="帖子/闲置"
                    leftText="取消"
                    rightText="发布"
                    onClickLeft={() => { navgiate("/gshow") }}
                    onClickRight={() => { tijiao() }}
                // onClickRight={() => { console.log(fid); }}
                />
            </div>
            <Input
                placeholder="分享好货，聊些新鲜事~"
                value={biaot}
                onChange={setBiao}
                clearable
                clearTrigger="always"
                style={{ marginTop: "20px", width: "79%", marginLeft: "10%" }}
            />
            <Input.TextArea
                style={{ marginTop: "40px", width: "79%", marginLeft: "10%", height: "60%" }}
                placeholder="清茶请客慢斟酌，久酿故事才香醇"
                value={neir}
                onChange={setNeir}
                showWordLimit={({ currentCount }) => <span>已经输入{currentCount}个字啦 ✍️</span>}
            />
            <div>
                <Uploader
                    url={uploadUrl}
                    style={{ marginTop: "140px", marginLeft: "10%" }}
                    onSuccess={(data) => {
                        setLj(JSON.parse(data.responseText).path)
                    }}
                />
                <Cell title='选择加入的宝库' isLink onClick={() => setVisible(true)} />
                <Dialog
                    visible={visible}
                    title='我加入的宝库'
                    showCancelButton
                    onConfirm={() => {
                        setVisible(false)
                    }}
                    onCancel={() => setVisible(false)}
                >
                    <div style={{ height: "100px" }}>
                        {
                            fid.map(el => (
                                <Button plain type='info' size='small' onClick={() => {
                                    setCid(el._id)
                                }} key={el._id} style={{ marginLeft: "10px", marginTop: "30px" }}>
                                    {el.title}
                                </Button>
                            ))
                        }
                    </div>
                </Dialog>
            </div>
        </div>
    )
}
